const path = require('path');
const packageName = require('./package.json').name;
// const CompressionWebpackPlugin = require('compression-webpack-plugin'); // 开启gzip压缩，按需引用
// const productionGzipExtensions = /\.(js|css|json|html|png|woff|ttf|eot)(\?.*)?$/i; // 开启gzip压缩，按需写入
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); // 压缩代码去掉console
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer'); // 打包分析
// const resolve = (dir) => path.join(__dirname, dir); // 获取相对路径
// const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
// const name = process.env.VUE_APP_TITLE || '测试项目'; // 项目名
const port = process.env.port || process.env.npm_config_port || 3000; // 端口

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  chainWebpack: config => {
    config.resolve.alias.set('@', path.resolve(__dirname, 'src'))
    config.resolve.symlinks(true); // 修复热更新
    config.output
      .library(`${packageName}-[name]`)
      .libraryTarget('umd')

    // 多页面打包查看html是否在结果数组中
    // config.plugin('html').tab(args => {
    //   args[0].title = name;
    //   // 修复路由懒加载
    //   args[0].chunksSortMode = 'none';
    //   return args
    // })
    // 打包分析
    // if (IS_PROD) {
    //   config.plugin('webpack-report').use(BundleAnalyzerPlugin, [{ analyserNode: 'static' }])
    // }
  },
  // configureWebpack: config => {
  //   const plugins = [];
  //   // 开启gzip压缩 npm i -D compression-webpack-plugin
  //   if (IS_PROD) {
  //     config.mode = 'production'
  //     plugins.push(
  //       new CompressionWebpackPlugin({
  //         filename: '[path][base].gz[query]',
  //         algorithm: 'gzip',
  //         test: productionGzipExtensions,
  //         threshold: 10240,
  //         deleteOriginalAssets: false
  //       })
  //     )
  //     plugins.push(
  //       new UglifyJsPlugin({
  //         output: { comments: true },
  //         compress: {
  //           drop_console: true,
  //           drop_debugger: false,
  //           pure_funcs: ['console.log']
  //         },
  //         warnings: false,
  //         parallel: true
  //       })
  //     )
  //   } else { config.mode = 'development' }
  //   config.plugins = [...config.plugins, ...plugins]
  // },
  // css: {
  //   extract: IS_PROD,
  //   loaderOptions: {
  //     less: {
  //       lessOptions: {
  //         javascriptEnabled: true,
  //         globalVars: {
  //           'hack': `true;@import "${resolove('src/assets/css/less/theme.less')}"`
  //         }
  //       }
  //     }
  //   }
  // },
  lintOnSave: false,
  devServer: {
    port,
    https: false,
    hot: true,
    client: {
      overlay: {
        errors: true,
        warnings: false,
      },
    },
    // disableHostCheck: true,
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': '*',
      'Access-Control-Allow-Credentials': 'true',
      'Access-Control-Allow-Methods': '*',
      'Cache-Control': 'no-cache'
    },
    proxy: {
      '/api': {
        target: 'http://192.168.1.7:8080',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '',
        },
      },
    }
  }
})
